package com.shop.cloud.controller.pay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.shop.cloud.SpringUtils;
import com.shop.cloud.config.ConfigUtil;
import com.shop.cloud.domain.hongfu.AlipayConfig;
import com.shop.cloud.service.hongfu.IAlipayConfigService;

/**
 * 支付宝支付后台配置
 * @author Yan Weiwei
 *
 */
@Component
public class AlibbpayConfig {

	private Logger logger = LoggerFactory.getLogger(AlibbpayConfig.class);
	
	@Autowired
	private IAlipayConfigService alipayConfigService;
	
	/**
	 * @param outTradeNo  订单系统中的唯一订单号匹配
	 * @param totalAmount 交易金额
	 * @param notifyUrl 交易过程中服务器通知的页面 要用 以http开格式的完整路径，不允许加?id=123这类自定义参数
	 * @return 订单字符串
	 * @throws AlipayApiException 
	 */
	public String getAlipayOrder(String outTradeNo, String totalAmount) throws AlipayApiException{
		
		AlipayConfig alipayConfig = alipayConfigService.get(new Long(1));
		
		//实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getServerUrl(), alipayConfig.getAppid(), alipayConfig.getAppPrivateKey(), "json", "UTF-8", alipayConfig.getAlipayPublicKey(), alipayConfig.getSignType());
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
		//SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody("");
		model.setSubject("充值余额");
		model.setOutTradeNo(outTradeNo);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(totalAmount);
		model.setProductCode("QUICK_MSECURITY_PAY");
		
		alipayRequest.setBizModel(model);
//		String url = SpringUtils.getBean(ConfigUtil.class).getAlipayCallbackUrl();
//		alipayRequest.setNotifyUrl(url + "api/alipayNotify");
		alipayRequest.setNotifyUrl(PayController.Alipay_Call_Back_Url+"api/alipayNotify");
		
		//这里和普通的接口调用不同，使用的是sdkExecute
		AlipayTradeAppPayResponse alipayResponse = alipayClient.sdkExecute(alipayRequest);
		String orderString = alipayResponse.getBody();
		logger.info("*******************alipay response data*********************"+orderString);
		return orderString;
	}
	
	/**
	 * @param outTradeNo  订单系统中的唯一订单号匹配
	 * @param totalAmount 交易金额
	 * @param notifyUrl 交易过程中服务器通知的页面 要用 以http开格式的完整路径，不允许加?id=123这类自定义参数
	 * @return 订单字符串
	 * @throws AlipayApiException 
	 */
	public String getAlipayOrderForPoints(String outTradeNo, String totalAmount) throws AlipayApiException{
		
		AlipayConfig alipayConfig = alipayConfigService.get(new Long(1));
		
		//实例化客户端
		AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getServerUrl(), alipayConfig.getAppid(), alipayConfig.getAppPrivateKey(), "json", "UTF-8", alipayConfig.getAlipayPublicKey(), alipayConfig.getSignType());
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
		//SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody("");
		model.setSubject("充值积分");
		model.setOutTradeNo(outTradeNo);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(totalAmount);
		model.setProductCode("QUICK_MSECURITY_PAY");
		
		alipayRequest.setBizModel(model);
//		String url = SpringUtils.getBean(ConfigUtil.class).getAlipayCallbackUrl();
//		alipayRequest.setNotifyUrl(url + "api/alipayNotify");
		alipayRequest.setNotifyUrl(PayController.Alipay_Call_Back_Url+"api/alipayNotifyForPoints");
		
		//这里和普通的接口调用不同，使用的是sdkExecute
		AlipayTradeAppPayResponse alipayResponse = alipayClient.sdkExecute(alipayRequest);
		String orderString = alipayResponse.getBody();
		logger.info("*******************alipay response data*********************"+orderString);
		return orderString;
	}
}
